﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Xml;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.LIB;
using YJKArchUtils.Utils;
using YJKArchUtils.Utils.SelectMaterial;
using YJKRevitKernel.FormUtility;
using YJKGeometryFuncs;
using YJKTools.PublicPreViewer;

namespace YJKRevitWallModule.WallModule.CreateWall
{
	public partial class DrawWallForm : System.Windows.Forms.Form
	{
		public DrawWallForm(Autodesk.Revit.DB.Document inputdoc, List<WallType> inputTypes, string strWallFSType = null)
		{
			this.m_strWallFSType = strWallFSType;
			this.m_doc = inputdoc;
			this.m_lstWallTypes = inputTypes;
			this.InitializeComponent();
			this.InitAllControls();
			this.m_controlManager = new ControlInfoManager(this, this.m_lstControls, null, null);
			base.ClientSize = new Size(this.NewTabControl.Width + 5, this.NewTabControl.Height + 7);
			this.m_mainPreviewer = new Previewer(this.NewTabPage, this.pnlPreview.Left, this.pnlPreview.Top, this.pnlPreview.Width, this.pnlPreview.Height, 1u);
			this.pnlPreview.Visible = false;
			this.m_mainCurPreviewer = new Previewer(this.CurrentTabPage, this.pnlCurPreview.Left, this.pnlCurPreview.Top, this.pnlCurPreview.Width, this.pnlCurPreview.Height, 1u);
			this.pnlCurPreview.Visible = false;
		}

		private void NewTabControl_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.SelectedIndexChangedRefresh();
		}

		private void SelectedIndexChangedRefresh()
		{
			if (this.NewTabControl.SelectedIndex == 0)
			{
				this.m_bIsEndCreateType = false;
				this.m_controlData.nPageNum = 0;
				base.ClientSize = new Size(this.NewTabControl.Width + 5, this.NewTabControl.Height + 10);
				return;
			}
			if (this.NewTabControl.SelectedIndex == 1)
			{
				this.m_bIsEndCreateType = true;
				this.m_controlData.nPageNum = 1;
				this.btnCurKeepWarm.Enabled = false;
				this.btnCurKeepWarm1.Enabled = false;
				this.numOutKeepWarm.Enabled = false;
				this.numInnerKeepWarm.Enabled = false;
				this.numWallThickness1.Enabled = false;
				this.numInnerKeepWarm.Enabled = false;
				this.cbCurUse.Enabled = false;
				this.cbCurMaterial.Enabled = false;
				base.ClientSize = new Size(this.NewTabControl.Width + this.CurPanel.Width + 9, this.NewTabControl.Height + 10);
				this.RefreshTree();
			}
		}

		private void LeftKeepWarmBtn_Click(object sender, EventArgs e)
		{
			this.m_bIsClickLeft = true;
			this.m_isChangeMaterial = true;
			this.PressEsc();
		}

		private void RightKeepWarmBtn_Click(object sender, EventArgs e)
		{
			this.m_bIsClickRight = true;
			this.m_isChangeMaterial = true;
			this.PressEsc();
		}

		public void ChangeMaterialType()
		{
			Transaction transaction = new Transaction(this.m_doc, "Change material");
			transaction.Start();
			ElementId elementId = null;
			if (MaterialSelectManager.MaterialSelect(this.m_doc, ElementId.InvalidElementId, ref elementId))
			{
				if (this.m_bIsClickRight)
				{
					this.m_controlData.strRightKeepWarmId = elementId;
					this.m_bIsClickRight = false;
				}
				if (this.m_bIsClickLeft)
				{
					this.m_controlData.strLeftKeepWarmId = elementId;
					this.m_bIsClickLeft = false;
				}
			}
			else
			{
				this.m_controlData.strRightKeepWarmId = ElementId.InvalidElementId;
				this.m_controlData.strLeftKeepWarmId = ElementId.InvalidElementId;
			}
			transaction.Commit();
		}

		private void treeWallType_AfterSelect(object sender, TreeViewEventArgs e)
		{
			double num = 0.0;
			WallType wallType = null;
			TreeNode selectedNode = this.treeWallType.SelectedNode;
			if (selectedNode != null && selectedNode.Name != "")
			{
				ElementId elementId = new ElementId(int.Parse(selectedNode.Name));
				wallType = (this.m_doc.GetElement(elementId) as WallType);
			}
			if (wallType == null)
			{
				return;
			}
			this.m_controlData.wallTypeId = wallType.Id;
            if ((int)wallType.Kind == 2 || (int)wallType.Kind == 1)
			{
				num = wallType.Width;
			}
			else if (wallType.Kind == 0)
			{
				int firstCoreLayerIndex = wallType.GetCompoundStructure().GetFirstCoreLayerIndex();
				int lastCoreLayerIndex = wallType.GetCompoundStructure().GetLastCoreLayerIndex();
				for (int i = firstCoreLayerIndex; i <= lastCoreLayerIndex; i++)
				{
					num += wallType.GetCompoundStructure().GetLayerWidth(i);
				}
			}
			this.numWallThickness1.Value = decimal.Parse((wallType.Width * DataDefine.m_dUnit).ToString());
			this.ChangeViewByData(num * DataDefine.m_dUnit, false);
			this.m_mainCurPreviewer.Refresh();
		}

		private void DrawWallForm_Load(object sender, EventArgs e)
		{
			if (this.m_lstLandPoints.Count == 0)
			{
				this.m_lstLandPoints.Add(new XYZ(3.2, 4.0, 0.0));
				this.m_lstLandPoints.Add(new XYZ(3.2, 2.0, 0.0));
				this.m_lstLandPoints.Add(new XYZ(4.0, 2.0, 0.0));
				this.m_lstLandPoints.Add(new XYZ(4.0, 4.0, 0.0));
			}
			if (this.m_lstCurLandPoints.Count == 0)
			{
				this.m_lstCurLandPoints.AddRange(this.m_lstLandPoints);
			}
			//base.Icon = Resource.YArchitechIcon;
			this.m_controlManager.SetControlInfoToForm();
			this.ReadXmlDataToForm();
			string s = this.numWallThickness.Value.ToString();
			this.ChangeViewByData(double.Parse(s), false);
			this.m_mainCurPreviewer.Refresh();
			if (this.m_strWallFSType != null)
			{
				this.SetTreeNodeByTypeNodeName(this.m_strWallFSType);
			}
		}

		private void DrawWallForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			try
			{
				this.m_controlManager.GetControlInfoToXml();
				this.WriteFileData();
			}
			catch (Exception)
			{
			}
			base.Dispose();
			this.PressEsc();
		}

		protected override void DefWndProc(ref Message m)
		{
			List<CurveInfo> curves = new List<CurveInfo>();
			int msg = m.Msg;
			if (msg == 1125)
			{
				if (m.WParam.ToInt32() != 1)
				{
					return;
				}
				try
				{
					curves = this.GetPreviewCurveInfo();
					if (!this.m_bIsEndCreateType)
					{
						this.m_mainPreviewer.Curves = curves;
					}
					else
					{
						this.m_mainCurPreviewer.Curves = curves;
					}
					return;
				}
				catch (Exception)
				{
					return;
				}
			}
			base.DefWndProc(ref m);
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		public void PressEsc()
		{
			this.m_bIsClickCLose = true;
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			int wMsg = 7;
			DrawWallForm.SendMessage(DrawWallManager._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			DrawWallForm.keybd_event((byte)bVk, 0, dwFlags, 0);
			DrawWallForm.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void numWallThickness_ValueChanged(object sender, EventArgs e)
		{
			if (this.numWallThickness.Value.ToString() == "0")
			{
				YJKMessageBox.Show("墙体核心层不能为0，请重新输入。");
				this.numWallThickness.Text = DataDefine.m_strDefaultWallWidth;
			}
			if (this.numWallThickness.Value.ToString() != null)
			{
				this.ChangeViewByData(double.Parse(this.numWallThickness.Value.ToString()), true);
				this.m_mainPreviewer.Refresh();
			}
		}

		private void numKeepWarmThickness_ValueChanged(object sender, EventArgs e)
		{
			if (this.numKeepWarmThickness.Value.ToString() != null)
			{
				this.ChangeKeepWarmCvByData();
				this.m_mainPreviewer.Refresh();
			}
		}

		private void numKeepWarmThickness2_ValueChanged(object sender, EventArgs e)
		{
			if (this.numKeepWarmThickness2.Value.ToString() != null)
			{
				this.ChangeKeepWarmCvByData();
				this.m_mainPreviewer.Refresh();
			}
		}

		private void cbLocationCv_TextChanged(object sender, EventArgs e)
		{
			this.m_location = this.GetLocationType(this.cbLocationCv.Text);
			if (this.m_bIsFlag)
			{
				this.m_mainPreviewer.Refresh();
			}
		}

		private void cbCurLocationCv_TextChanged(object sender, EventArgs e)
		{
			this.m_curLocation = this.GetLocationType(this.cbCurLocationCv.Text);
			if (this.m_bIsFlag)
			{
				this.m_mainCurPreviewer.Refresh();
			}
		}

		private void numWallThickness_KeyUp(object sender, KeyEventArgs e)
		{
			string text = this.numWallThickness.Value.ToString();
			if (this.numWallThickness.Value.ToString() == "0")
			{
				YJKMessageBox.Show("墙体核心层不能为0，请重新输入。");
				this.numWallThickness.Text = DataDefine.m_strDefaultWallWidth;
				return;
			}
			this.numWallThickness.Text = text;
		}

		private void numKeepWarmThickness_KeyUp(object sender, KeyEventArgs e)
		{
			this.ChangeKeepWarmCvByData();
			this.m_mainPreviewer.Refresh();
		}

		private void numKeepWarmThickness2_KeyUp(object sender, KeyEventArgs e)
		{
			this.ChangeKeepWarmCvByData();
			this.m_mainPreviewer.Refresh();
		}

		private void cbWallHeightKeyPress(object sender, KeyPressEventArgs e)
		{
			if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != '\b')
			{
				e.Handled = true;
				return;
			}
			if (this.cbWallHeight.Text == "" && this.cbWallHeight.Text.Length == 0 && e.KeyChar == '0')
			{
				e.Handled = true;
				return;
			}
		}

		private void cbWallHeight_KeyUp(object sender, KeyEventArgs e)
		{
			if (this.cbWallHeight.Text == "0")
			{
				YJKMessageBox.Show("墙体高度不能为0，请重新输入。");
				this.cbWallHeight.Text = "当前层高";
			}
		}

		private void cbCurWallHeight_KeyUp(object sender, KeyEventArgs e)
		{
			if (this.cbCurWallHeight.Text == "0")
			{
				YJKMessageBox.Show("墙体高度不能为0，请重新输入。");
				this.cbCurWallHeight.Text = "当前层高";
			}
		}

		private void cbCurWallHeight_KeyPress(object sender, KeyPressEventArgs e)
		{
			if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != '\b')
			{
				e.Handled = true;
				return;
			}
			if (this.cbCurWallHeight.Text == "" && this.cbCurWallHeight.Text.Length == 0 && e.KeyChar == '0')
			{
				e.Handled = true;
				return;
			}
		}

		private void tbOffset_KeyPress(object sender, KeyPressEventArgs e)
		{
			if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != '\b' && e.KeyChar != '-')
			{
				e.Handled = true;
				return;
			}
			if (this.tbOffset.Text == "" && this.tbOffset.Text.Length == 0 && e.KeyChar == '0')
			{
				e.Handled = true;
				return;
			}
		}

		private void tbCurOffset_KeyPress(object sender, KeyPressEventArgs e)
		{
			if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != '\b' && e.KeyChar != '-')
			{
				e.Handled = true;
				return;
			}
			if (this.tbCurOffset.Text == "" && this.tbCurOffset.Text.Length == 0 && e.KeyChar == '0')
			{
				e.Handled = true;
				return;
			}
		}

		private void treeWallType_DrawNode(object sender, DrawTreeNodeEventArgs e)
		{
			if ((e.State & TreeNodeStates.Selected) != (TreeNodeStates)0)
			{
				e.Graphics.FillRectangle(Brushes.DodgerBlue, e.Node.Bounds);
				Font font = e.Node.NodeFont;
				if (font == null)
				{
					font = ((TreeView)sender).Font;
				}
				e.Graphics.DrawString(e.Node.Text, font, Brushes.White, System.Drawing.Rectangle.Inflate(e.Bounds, 2, 0));
			}
			else
			{
				e.DrawDefault = true;
			}
			if ((e.State & TreeNodeStates.Focused) != (TreeNodeStates)0)
			{
				using (Pen pen = new Pen(System.Drawing.Color.Black))
				{
					pen.DashStyle = DashStyle.Dot;
					System.Drawing.Rectangle bounds = e.Node.Bounds;
					bounds.Size = new Size(bounds.Width - 1, bounds.Height - 1);
					e.Graphics.DrawRectangle(pen, bounds);
				}
			}
		}

		private void cbWallHeightMouseLeave(object sender, EventArgs e)
		{
			bool flag = false;
			if (!this.m_bIsEndCreateType && this.cbWallHeight.Text == "")
			{
				YJKMessageBox.Show("不能为空，请重新输入!");
				return;
			}
			if (this.m_bIsEndCreateType && this.cbCurWallHeight.Text == "")
			{
				YJKMessageBox.Show("不能为空，请重新输入!");
				return;
			}
			base.OnMouseLeave(e);
			if (!this.m_bIsEndCreateType)
			{
				if (this.cbWallHeight.Text != "" && !this.m_lstHeight.Contains(this.cbWallHeight.Text))
				{
					flag = true;
					this.m_lstHeight.RemoveAt(this.nIndex);
					this.m_lstHeight.Insert(this.nIndex, this.cbWallHeight.Text);
					this.nIndex++;
					if (this.nIndex > 9)
					{
						this.nIndex = 1;
					}
				}
				if (this.m_bIsFlag && flag)
				{
					this.cbWallHeight.Items.Clear();
					foreach (string item in this.m_lstHeight)
					{
						this.cbWallHeight.Items.Add(item);
					}
					this.cbWallHeight.Refresh();
					return;
				}
			}
			else
			{
				if (this.cbCurWallHeight.Text != "" && !this.m_lstCurrentHeight.Contains(this.cbCurWallHeight.Text))
				{
					flag = true;
					this.m_lstCurrentHeight.RemoveAt(this.nCurIndex);
					this.m_lstCurrentHeight.Insert(this.nCurIndex, this.cbCurWallHeight.Text);
					this.nCurIndex++;
					if (this.nCurIndex > 9)
					{
						this.nCurIndex = 1;
					}
				}
				if (this.m_bIsFlag && flag)
				{
					this.cbCurWallHeight.Items.Clear();
					foreach (string item2 in this.m_lstCurrentHeight)
					{
						this.cbCurWallHeight.Items.Add(item2);
					}
					this.cbCurWallHeight.Refresh();
				}
			}
		}

		private void InitAllControls()
		{
			this.InitTree();
			this.InitCombobox();
			this.GetRememberControls();
		}

		private void SetTreeNodeByTypeNodeName(string strWallFSType)
		{
			this.NewTabControl.SelectedIndex = 1;
			this.SelectedIndexChangedRefresh();
			foreach (object obj in this.treeWallType.Nodes)
			{
				foreach (object obj2 in ((TreeNode)obj).Nodes)
				{
					TreeNode treeNode = (TreeNode)obj2;
					if (!(treeNode.Text != strWallFSType))
					{
						if (this.treeWallType.SelectedNode != null)
						{
							this.treeWallType.SelectedNode.Checked = false;
						}
						treeNode.Checked = true;
						this.treeWallType.SelectedNode = treeNode;
						return;
					}
				}
			}
		}

		private void InitCombobox()
		{
			foreach (string item in DataDefine.m_useArray)
			{
				this.cbUse.Items.Add(item);
				this.cbCurUse.Items.Add(item);
			}
			foreach (string item2 in DataDefine.m_materialArray)
			{
				this.cbMaterial.Items.Add(item2);
			}
			foreach (string item3 in DataDefine.m_locationArray)
			{
				this.cbLocationCv.Items.Add(item3);
				this.cbCurLocationCv.Items.Add(item3);
			}
			foreach (string item4 in DataDefine.m_wallHeight)
			{
				if (this.cbWallHeight.Items.Count > 0 || this.cbCurWallHeight.Items.Count > 0)
				{
					break;
				}
				this.cbWallHeight.Items.Add(item4);
				this.cbCurWallHeight.Items.Add(item4);
			}
			foreach (string item5 in DataDefine.m_wallHeight)
			{
				this.m_lstHeight.Add(item5);
				this.m_lstCurrentHeight.Add(item5);
			}
			this.cbUse.SelectedIndex = 0;
			this.cbCurUse.SelectedIndex = 0;
			this.cbMaterial.SelectedIndex = 0;
			this.cbLocationCv.SelectedIndex = 0;
			this.cbCurLocationCv.SelectedIndex = 0;
			this.cbWallHeight.SelectedIndex = 0;
			this.cbCurWallHeight.SelectedIndex = 0;
			this.m_bIsFlag = true;
		}

		private void InitTree()
		{
			this.treeWallType.Nodes.Clear();
			TreeNode treeNode = this.treeWallType.Nodes.Add(DataDefine.m_strBaseWall);
			TreeNode treeNode2 = this.treeWallType.Nodes.Add(DataDefine.m_strLaminatedWall);
			TreeNode treeNode3 = this.treeWallType.Nodes.Add(DataDefine.m_strCurtainWall);
			foreach (WallType wallType in this.m_lstWallTypes)
			{
				string name = wallType.Name;
				string key = wallType.Id.IntegerValue.ToString();
				if (wallType.Kind == 0)
				{
					treeNode.Nodes.Add(key, name);
				}
                else if ((int)wallType.Kind == 2)
				{
					treeNode2.Nodes.Add(key, name);
				}
                else if ((int)wallType.Kind == 1)
				{
					treeNode3.Nodes.Add(key, name);
				}
			}
			this.treeWallType.ExpandAll();
			if (this.m_treeNodeElementId == null)
			{
				this.treeWallType.SelectedNode = this.treeWallType.Nodes[0].FirstNode;
				return;
			}
			foreach (object obj in this.treeWallType.Nodes)
			{
				foreach (object obj2 in ((TreeNode)obj).Nodes)
				{
					TreeNode treeNode4 = (TreeNode)obj2;
					if (int.Parse(treeNode4.Name) == this.m_treeNodeElementId.IntegerValue)
					{
						this.treeWallType.SelectedNode = treeNode4;
						break;
					}
				}
			}
		}

		private void RefreshTree()
		{
			List<WallType> list = new List<WallType>();
			foreach (Element element in new FilteredElementCollector(this.m_doc).OfClass(typeof(WallType)).ToElements())
			{
				WallType wallType = element as WallType;
				if (wallType != null)
				{
					list.Add(wallType);
				}
			}
			this.m_lstWallTypes = list;
			this.InitTree();
		}

		private void GetRememberControls()
		{
			foreach (object obj in base.Controls)
			{
				foreach (object obj2 in ((System.Windows.Forms.Control)obj).Controls)
				{
					System.Windows.Forms.Control control = (System.Windows.Forms.Control)obj2;
					if (control is TabPage)
					{
						foreach (object obj3 in control.Controls)
						{
							System.Windows.Forms.Control control2 = (System.Windows.Forms.Control)obj3;
							if (control2 is System.Windows.Forms.ComboBox)
							{
								this.m_lstControls.Add(new KeyValuePair<System.Windows.Forms.Control, bool>(control2, false));
							}
						}
					}
				}
			}
		}

		private List<CurveInfo> GetPreviewCurveInfo()
		{
			List<CurveInfo> list = new List<CurveInfo>();
			list = this.CreateLandEdgeloop();
			list.Add(this.CreateLocationCv());
			if (this.NewTabControl.SelectedIndex == 0)
			{
				list.AddRange(this.CreateKeepWarmCv());
			}
			return list;
		}

		private List<CurveInfo> CreateKeepWarmCv()
		{
			List<CurveInfo> result = new List<CurveInfo>();
			if (this.NewTabControl.SelectedIndex == 0)
			{
				this.CreateNewPageKeepWarmCv(ref result, true);
			}
			else if (this.NewTabControl.SelectedIndex == 1)
			{
				WallType wallType = null;
				TreeNode selectedNode = this.treeWallType.SelectedNode;
				if (selectedNode != null && selectedNode.Name != null)
				{
					ElementId elementId = new ElementId(int.Parse(selectedNode.Name));
					wallType = (this.m_doc.GetElement(elementId) as WallType);
				}
                if (wallType != null && (int)wallType.Kind == 1)
				{
					return result;
				}
				this.CreateNewPageKeepWarmCv(ref result, false);
			}
			return result;
		}

		private void CreateNewPageKeepWarmCv(ref List<CurveInfo> lstCvInfo, bool bIsNewPage = true)
		{
			if (bIsNewPage)
			{
				List<XYZ> list = new List<XYZ>();
				list.AddRange(this.m_lstLandPoints);
				if (this.numKeepWarmThickness.Value.ToString() != DataDefine.m_strKeepWarmThickness)
				{
					XYZ ptS = new XYZ(list[0].X - DataDefine.MMToViewData(this.m_controlData.strLeftKeepWarmWidth), list[0].Y, list[0].Z);
					XYZ ptE = new XYZ(list[1].X - DataDefine.MMToViewData(this.m_controlData.strLeftKeepWarmWidth), list[1].Y, list[1].Z);
					lstCvInfo.Add(this.NewLine(PenColor.PC_Pink, 2f, ptS, ptE));
				}
				if (this.numKeepWarmThickness2.Value.ToString() != DataDefine.m_strKeepWarmThickness)
				{
					XYZ ptS2 = new XYZ(list[2].X + DataDefine.MMToViewData(this.m_controlData.strRightKeepWarmWidth), list[2].Y, list[2].Z);
					XYZ ptE2 = new XYZ(list[3].X + DataDefine.MMToViewData(this.m_controlData.strRightKeepWarmWidth), list[3].Y, list[3].Z);
					lstCvInfo.Add(this.NewLine(PenColor.PC_Pink, 2f, ptS2, ptE2));
					return;
				}
			}
			else
			{
				List<XYZ> list2 = new List<XYZ>();
				list2.AddRange(this.m_lstCurLandPoints);
				XYZ ptS3 = new XYZ(list2[0].X - DataDefine.m_dExtendLocationCv, list2[0].Y, list2[0].Z);
				XYZ ptE3 = new XYZ(list2[1].X - DataDefine.m_dExtendLocationCv, list2[1].Y, list2[1].Z);
				lstCvInfo.Add(this.NewLine(PenColor.PC_Pink, 2f, ptS3, ptE3));
				XYZ ptS4 = new XYZ(list2[2].X + DataDefine.m_dExtendLocationCv, list2[2].Y, list2[2].Z);
				XYZ ptE4 = new XYZ(list2[3].X + DataDefine.m_dExtendLocationCv, list2[3].Y, list2[3].Z);
				lstCvInfo.Add(this.NewLine(PenColor.PC_Pink, 2f, ptS4, ptE4));
			}
		}

		private CurveInfo CreateLocationCv()
		{
			CurveInfo result = default(CurveInfo);
			if (this.NewTabControl.SelectedIndex == 0)
			{
				this.CreateNewPagePreviewCv(this.m_location, ref result, true);
                //result.CurveObj != null;
				return result;
			}
			if (this.NewTabControl.SelectedIndex == 1)
			{
				this.CreateNewPagePreviewCv(this.m_curLocation, ref result, false);
                //result.CurveObj != null;
				return result;
			}
			return result;
		}

		private void CreateNewPagePreviewCv(LocationCv m_location, ref CurveInfo curve, bool bIsNewPage = true)
		{
			double num = 0.0;
			List<XYZ> list = new List<XYZ>();
			double num2;
			if (bIsNewPage)
			{
				list.AddRange(this.m_lstLandPoints);
				num2 = DataDefine.MMToViewData(this.numKeepWarmThickness.Value.ToString());
				num = DataDefine.MMToViewData(this.numKeepWarmThickness2.Value.ToString());
			}
			else
			{
				list.AddRange(this.m_lstCurLandPoints);
				num2 = DataDefine.m_dExtendLocationCv;
			}
			XYZ xyz = YJKGeometryFuncs.Geometry.CalculatMidPoint(list[0], list[3]);
			XYZ xyz2 = YJKGeometryFuncs.Geometry.CalculatMidPoint(list[1], list[2]);
			xyz = new XYZ(xyz.X, xyz.Y + DataDefine.m_dExtendLocationCv, xyz.Z);
			xyz2 = new XYZ(xyz2.X, xyz2.Y - DataDefine.m_dExtendLocationCv, xyz2.Z);
			switch (m_location)
			{
			case LocationCv.WallCenter:
				if (bIsNewPage)
				{
					this.CreateWallCenterCv(ref curve);
					return;
				}
				curve = this.NewLine(PenColor.PC_Red, 2f, xyz, xyz2);
				return;
			case LocationCv.CoreCenter:
				curve = this.NewLine(PenColor.PC_Red, 2f, xyz, xyz2);
				return;
			case LocationCv.SurfaceLayerOuter:
				xyz = new XYZ(list[0].X - num2, xyz.Y, xyz.Z);
				xyz2 = new XYZ(list[1].X - num2, xyz2.Y, xyz2.Z);
				curve = this.NewLine(PenColor.PC_Red, 2f, xyz, xyz2);
				return;
			case LocationCv.SurfaceLayerInner:
				xyz = new XYZ(list[3].X + num, xyz.Y, xyz.Z);
				xyz2 = new XYZ(list[2].X + num, xyz2.Y, xyz2.Z);
				curve = this.NewLine(PenColor.PC_Red, 2f, xyz, xyz2);
				return;
			case LocationCv.CoreLayerOuter:
				xyz = new XYZ(list[0].X, xyz.Y, xyz.Z);
				xyz2 = new XYZ(list[1].X, xyz2.Y, xyz2.Z);
				curve = this.NewLine(PenColor.PC_Red, 2f, xyz, xyz2);
				return;
			case LocationCv.CoreLayerInner:
				xyz = new XYZ(list[3].X, xyz.Y, xyz.Z);
				xyz2 = new XYZ(list[2].X, xyz2.Y, xyz2.Z);
				curve = this.NewLine(PenColor.PC_Red, 2f, xyz, xyz2);
				return;
			default:
				return;
			}
		}

		private void CreateWallCenterCv(ref CurveInfo CvInfo)
		{
			List<XYZ> list = new List<XYZ>();
			list.AddRange(this.m_lstLandPoints);
			double num = DataDefine.MMToViewData(this.numKeepWarmThickness.Value.ToString());
			double num2 = DataDefine.MMToViewData(this.numKeepWarmThickness2.Value.ToString());
			list[0] = new XYZ(list[0].X - num, list[0].Y, list[0].Z);
			list[1] = new XYZ(list[1].X - num, list[1].Y, list[1].Z);
			list[2] = new XYZ(list[2].X + num2, list[2].Y, list[2].Z);
			list[3] = new XYZ(list[3].X + num2, list[3].Y, list[3].Z);
			XYZ xyz = YJKGeometryFuncs.Geometry.CalculatMidPoint(list[0], list[3]);
			XYZ xyz2 = YJKGeometryFuncs.Geometry.CalculatMidPoint(list[1], list[2]);
			xyz = new XYZ(xyz.X, xyz.Y + DataDefine.m_dExtendLocationCv, xyz.Z);
			xyz2 = new XYZ(xyz2.X, xyz2.Y - DataDefine.m_dExtendLocationCv, xyz2.Z);
			CvInfo = this.NewLine(PenColor.PC_Red, 2f, xyz, xyz2);
		}

		private List<CurveInfo> CreateLandEdgeloop()
		{
			List<XYZ> list = new List<XYZ>();
			List<CurveInfo> list2 = new List<CurveInfo>();
			if (!this.m_bIsEndCreateType)
			{
				list.AddRange(this.m_lstLandPoints);
			}
			else
			{
				list.AddRange(this.m_lstCurLandPoints);
			}
			if (list.Count > 2)
			{
				for (int i = 0; i < list.Count - 1; i++)
				{
					CurveInfo item = this.NewLine(PenColor.PC_Black, 2f, list[i], list[i + 1]);
					if (item.CurveObj != null)
					{
						list2.Add(item);
					}
				}
				int index = list.Count - 1;
				CurveInfo item2 = this.NewLine(PenColor.PC_Black, 2f, list[index], list[0]);
				if (item2.CurveObj != null)
				{
					list2.Add(item2);
				}
			}
			return list2;
		}

		private void ChangeViewByData(double dWallThickness, bool bIsNewPage = true)
		{
			double num = 1.0;
			if (dWallThickness == 0.0)
			{
				return;
			}
			this.m_controlData.strWallWidth = dWallThickness.ToString();
			Line line;
			XYZ item;
			if (bIsNewPage)
			{
				if (this.m_lstLandPoints.Count == 0)
				{
					return;
				}
				line = Line.CreateBound(this.m_lstLandPoints[1], this.m_lstLandPoints[2]);
				item = this.m_lstLandPoints[0];
				this.m_lstLandPoints.Clear();
			}
			else
			{
				if (this.m_lstCurLandPoints.Count == 0)
				{
					return;
				}
				line = Line.CreateBound(this.m_lstCurLandPoints[1], this.m_lstCurLandPoints[2]);
				item = this.m_lstCurLandPoints[0];
				this.m_lstCurLandPoints.Clear();
			}
			double num2 = line.Length;
			if (this.m_controlData.strWallWidth != "" && this.m_controlData.strWallWidth != this.m_strWallWidth)
			{
				num = double.Parse(this.m_controlData.strWallWidth) / double.Parse(this.m_strWallWidth);
			}
			this.m_strWallWidth = dWallThickness.ToString();
			num2 *= num;
			if (bIsNewPage)
			{
				this.m_lstLandPoints.Add(item);
				this.m_lstLandPoints.Add(new XYZ(3.2, 2.0, 0.0));
				this.m_lstLandPoints.Add(new XYZ(3.2 + num2, 2.0, 0.0));
				this.m_lstLandPoints.Add(new XYZ(3.2 + num2, 4.0, 0.0));
				return;
			}
			this.m_lstCurLandPoints.Add(item);
			this.m_lstCurLandPoints.Add(new XYZ(3.2, 2.0, 0.0));
			this.m_lstCurLandPoints.Add(new XYZ(3.2 + num2, 2.0, 0.0));
			this.m_lstCurLandPoints.Add(new XYZ(3.2 + num2, 4.0, 0.0));
		}

		private void ChangeKeepWarmCvByData()
		{
			if (this.numKeepWarmThickness.Value.ToString() != null)
			{
				this.m_controlData.strLeftKeepWarmWidth = this.numKeepWarmThickness.Value.ToString();
			}
			if (this.numKeepWarmThickness2.Value.ToString() != null)
			{
				this.m_controlData.strRightKeepWarmWidth = this.numKeepWarmThickness2.Value.ToString();
			}
		}

		private CurveInfo NewLine(PenColor color, float m_LineWidth1, XYZ ptS, XYZ ptE)
		{
			Transform identity = Transform.Identity;
			Transform identity2 = Transform.Identity;
			Transform identity3 = Transform.Identity;
			Transform transform = identity.Multiply(identity2);
			transform = transform.Multiply(identity3);
			XYZ xyz = YJKArchUtils.Utils.Geometry.TransformPoint(ptS, transform);
			XYZ xyz2 = YJKArchUtils.Utils.Geometry.TransformPoint(ptE, transform);
			Line curve = null;
			CurveInfo result;
			try
			{
				curve = Line.CreateBound(xyz, xyz2);
				result = new CurveInfo(curve, color, m_LineWidth1);
			}
			catch (Exception)
			{
				result = new CurveInfo(curve, color, m_LineWidth1);
			}
			return result;
		}

		private LocationCv GetLocationType(string strInput)
		{
			LocationCv result = LocationCv.WallCenter;
			if (strInput == "墙中心线")
			{
				result = LocationCv.WallCenter;
			}
			else if (strInput == "核心层中心线")
			{
				result = LocationCv.CoreCenter;
			}
			else if (strInput == "面层面：外部")
			{
				result = LocationCv.SurfaceLayerOuter;
			}
			else if (strInput == "面层面：内部")
			{
				result = LocationCv.SurfaceLayerInner;
			}
			else if (strInput == "核心面：外部")
			{
				result = LocationCv.CoreLayerOuter;
			}
			else if (strInput == "核心面：内部")
			{
				result = LocationCv.CoreLayerInner;
			}
			return result;
		}

		public UseData GetUseData()
		{
			UseData result = default(UseData);
			result.strLeftKeepWarmWidth = this.numKeepWarmThickness.Value.ToString();
			result.strRightKeepWarmWidth = this.numKeepWarmThickness2.Value.ToString();
			result.strWallWidth = this.numWallThickness.Value.ToString();
			result.strPurpose = this.cbUse.Text;
			result.strMaterial2FillName = this.GetFillMaterialName(this.cbMaterial.Text);
			if (this.m_bIsEndCreateType)
			{
				if (this.tbCurOffset.Text == "")
				{
					this.tbCurOffset.Text = "0";
				}
				result.strWallOffset = this.tbCurOffset.Text;
				result.wallTypeId = this.m_controlData.wallTypeId;
				result.strWallHeight = this.cbCurWallHeight.Text;
				result.location = this.GetLocationType(this.cbCurLocationCv.Text);
			}
			else
			{
				if (this.tbOffset.Text == "")
				{
					this.tbOffset.Text = "0";
				}
				result.strWallOffset = this.tbOffset.Text;
				result.wallTypeId = ElementId.InvalidElementId;
				result.strWallHeight = this.cbWallHeight.Text;
				result.location = this.GetLocationType(this.cbLocationCv.Text);
			}
			result.strLeftKeepWarmId = this.m_controlData.strLeftKeepWarmId;
			result.strRightKeepWarmId = this.m_controlData.strRightKeepWarmId;
			return result;
		}

		private KeyValuePair<string, string> GetFillMaterialName(string inputMaterialName)
		{
			KeyValuePair<string, string> result = default(KeyValuePair<string, string>);
			if (inputMaterialName == "钢筋混凝土")
			{
				result = new KeyValuePair<string, string>(inputMaterialName, "混凝土-钢砼");
			}
			else if (inputMaterialName == "混凝土")
			{
				result = new KeyValuePair<string, string>(inputMaterialName, "混凝土");
			}
			else if (inputMaterialName == "砖")
			{
				result = new KeyValuePair<string, string>(inputMaterialName, "砌体 - 砖");
			}
			else if (inputMaterialName == "耐火砖")
			{
				result = new KeyValuePair<string, string>(inputMaterialName, "砌体-耐火砖");
			}
			else if (inputMaterialName == "石材")
			{
				result = new KeyValuePair<string, string>(inputMaterialName, "石材-剖面纹理");
			}
			else if (inputMaterialName == "毛石")
			{
				result = new KeyValuePair<string, string>(inputMaterialName, "砌体-毛石");
			}
			else if (inputMaterialName == "填充墙")
			{
				result = new KeyValuePair<string, string>(inputMaterialName, "砌体 - 混凝土砌块");
			}
			else if (inputMaterialName == "空心砖")
			{
				result = new KeyValuePair<string, string>(inputMaterialName, "砌体-空心砖剖面");
			}
			else if (inputMaterialName == "加气砖")
			{
				result = new KeyValuePair<string, string>(inputMaterialName, "砌体-加气砼");
			}
			else if (inputMaterialName == "石膏板")
			{
				result = new KeyValuePair<string, string>(inputMaterialName, "石膏 - 灰泥");
			}
			return result;
		}

		private XmlElement FindXmlData()
		{
			XmlElement result = null;
			string filename = Product.DataLocation + DataDefine.m_wallDataFileName;
			this.tempPath = filename;
			try
			{
				this.xmlDoc = new XmlDocument();
				this.xmlDoc.Load(filename);
				result = this.xmlDoc.DocumentElement;
			}
			catch (Exception)
			{
				YJKMessageBox.Show("初始化用户数据文件(" + DataDefine.m_wallDataFileName + ")失败。");
			}
			return result;
		}

		private void WriteFileData()
		{
			foreach (object obj in this.FindXmlData().ChildNodes)
			{
				XmlNode xmlNode = (XmlNode)obj;
				if (xmlNode.Name == "cbWallHeight" && xmlNode is XmlElement)
				{
					(xmlNode as XmlElement).SetAttribute("value", this.BuildString(this.m_lstHeight));
				}
				else if (xmlNode.Name == "cbCurWallHeight" && xmlNode is XmlElement)
				{
					(xmlNode as XmlElement).SetAttribute("value", this.BuildString(this.m_lstCurrentHeight));
				}
				else if (xmlNode.Name == "numKeepWarmThickness" && xmlNode is XmlElement)
				{
					(xmlNode as XmlElement).SetAttribute("value", this.numKeepWarmThickness.Value.ToString());
				}
				else if (xmlNode.Name == "numWallThickness" && xmlNode is XmlElement)
				{
					(xmlNode as XmlElement).SetAttribute("value", this.numWallThickness.Value.ToString());
				}
				else if (xmlNode.Name == "numKeepWarmThickness2" && xmlNode is XmlElement)
				{
					(xmlNode as XmlElement).SetAttribute("value", this.numKeepWarmThickness2.Value.ToString());
				}
				else if (xmlNode.Name == "outerKeepWarmId" && xmlNode is XmlElement)
				{
					(xmlNode as XmlElement).SetAttribute("value", this.m_controlData.strLeftKeepWarmId.IntegerValue.ToString());
				}
				else if (xmlNode.Name == "innerKeepWarmId" && xmlNode is XmlElement)
				{
					(xmlNode as XmlElement).SetAttribute("value", this.m_controlData.strRightKeepWarmId.IntegerValue.ToString());
				}
				else if (xmlNode.Name == "TabPageVal" && xmlNode is XmlElement)
				{
					(xmlNode as XmlElement).SetAttribute("value", this.m_controlData.nPageNum.ToString());
				}
				else if (xmlNode.Name == "CurWallTypeId" && xmlNode is XmlElement)
				{
					(xmlNode as XmlElement).SetAttribute("value", this.m_controlData.wallTypeId.IntegerValue.ToString());
				}
				else if (xmlNode.Name == "tbOffsetVal")
				{
					(xmlNode as XmlElement).SetAttribute("value", this.tbOffset.Text);
				}
				else if (xmlNode.Name == "tbCurOffsetVal")
				{
					(xmlNode as XmlElement).SetAttribute("value", this.tbCurOffset.Text);
				}
			}
			this.xmlDoc.Save(this.tempPath);
		}

		private void ReadXmlDataToForm()
		{
			XmlElement xmlElement = this.FindXmlData();
			new List<string>();
			string empty = string.Empty;
			if (xmlElement == null)
			{
				return;
			}
			foreach (object obj in xmlElement.ChildNodes)
			{
				XmlNode xmlNode = (XmlNode)obj;
				new List<string>();
				if (xmlNode.Name == "cbWallHeight")
				{
					this.cbWallHeight.Items.Clear();
					using (List<string>.Enumerator enumerator2 = this.Substr((xmlNode as XmlElement).GetAttribute("value")).GetEnumerator())
					{
						while (enumerator2.MoveNext())
						{
							string item = enumerator2.Current;
							this.cbWallHeight.Items.Add(item);
						}
						continue;
					}
				}
				if (xmlNode.Name == "cbCurWallHeight")
				{
					this.cbCurWallHeight.Items.Clear();
					using (List<string>.Enumerator enumerator2 = this.Substr((xmlNode as XmlElement).GetAttribute("value")).GetEnumerator())
					{
						while (enumerator2.MoveNext())
						{
							string item2 = enumerator2.Current;
							this.cbCurWallHeight.Items.Add(item2);
						}
						continue;
					}
				}
				if (xmlNode.Name == "numKeepWarmThickness")
				{
					this.numKeepWarmThickness.Text = (xmlNode as XmlElement).GetAttribute("value");
				}
				else if (xmlNode.Name == "numWallThickness")
				{
					this.numWallThickness.Text = (xmlNode as XmlElement).GetAttribute("value");
				}
				else if (xmlNode.Name == "numKeepWarmThickness2")
				{
					this.numKeepWarmThickness2.Text = (xmlNode as XmlElement).GetAttribute("value");
				}
				else if (xmlNode.Name == "outerKeepWarmId")
				{
					this.m_controlData.strLeftKeepWarmId = new ElementId(int.Parse((xmlNode as XmlElement).GetAttribute("value")));
				}
				else if (xmlNode.Name == "innerKeepWarmId")
				{
					this.m_controlData.strRightKeepWarmId = new ElementId(int.Parse((xmlNode as XmlElement).GetAttribute("value")));
				}
				else if (xmlNode.Name == "TabPageVal")
				{
					this.m_controlData.nPageNum = int.Parse((xmlNode as XmlElement).GetAttribute("value"));
					this.NewTabControl.SelectedIndex = this.m_controlData.nPageNum;
				}
				else if (xmlNode.Name == "tbOffsetVal")
				{
					string attribute = (xmlNode as XmlElement).GetAttribute("value");
					this.tbOffset.Text = attribute;
				}
				else if (xmlNode.Name == "tbCurOffsetVal")
				{
					string attribute2 = (xmlNode as XmlElement).GetAttribute("value");
					this.tbCurOffset.Text = attribute2;
				}
				else if (xmlNode.Name == "CurWallTypeId")
				{
					this.m_controlData.wallTypeId = new ElementId(int.Parse((xmlNode as XmlElement).GetAttribute("value")));
					try
					{
						if (this.m_doc.GetElement(this.m_controlData.wallTypeId) == null)
						{
							this.m_controlData.wallTypeId = new ElementId(int.Parse(this.treeWallType.Nodes[0].FirstNode.Name));
						}
					}
					catch (Exception)
					{
					}
					this.m_treeNodeElementId = this.m_controlData.wallTypeId;
				}
			}
		}

		private string BuildString(List<string> lststr)
		{
			string text = null;
			foreach (string str in lststr)
			{
				text = text + str + ",";
			}
			return text;
		}

		private List<string> Substr(string inputstr)
		{
			List<string> list = new List<string>();
			while (!(inputstr == ","))
			{
				int num = inputstr.IndexOf(",");
				if (num == -1)
				{
					break;
				}
				list.Add(inputstr.Substring(0, num));
				inputstr = inputstr.Substring(num).Remove(0, 1);
			}
			return list;
		}

		public bool m_isExist = true;

		public bool m_isChangeMaterial;

		public List<WallType> m_lstWallTypes = new List<WallType>();

		private string m_strWallWidth = "200";

		private Document m_doc;

		private ControlData m_controlData;

		private ElementId m_treeNodeElementId;

		private LocationCv m_location;

		private LocationCv m_curLocation;

		private bool m_bIsFlag;

		private bool m_bIsEndCreateType;

		public bool m_bIsClickCLose;

		private List<string> m_lstHeight = new List<string>();

		private List<string> m_lstCurrentHeight = new List<string>();

		private Previewer m_mainPreviewer;

		private Previewer m_mainCurPreviewer;

		private List<XYZ> m_lstLandPoints = new List<XYZ>();

		private List<XYZ> m_lstCurLandPoints = new List<XYZ>();

		private bool m_bIsClickLeft;

		private bool m_bIsClickRight;

		private ControlInfoManager m_controlManager;

		private List<KeyValuePair<System.Windows.Forms.Control, bool>> m_lstControls = new List<KeyValuePair<System.Windows.Forms.Control, bool>>();

		private string m_strWallFSType;

		private int nIndex = 1;

		private int nCurIndex = 1;

		private string tempPath;

		private XmlDocument xmlDoc;
	}
}
